package com.boe.common.util;

import lombok.*;
import java.util.Random;

public class RandomUtil {
    private static Random random = new Random(System.currentTimeMillis());

    /**
     * 获取指定长度的随机字符串
     *
     * @param length
     * @return
     */
    public static String getRandomWithLength(Integer length) {
        StringBuilder retBuilder = new StringBuilder();
        while (true) {
            retBuilder.append(Integer.toString(Math.abs(random.nextInt()), 36));
            if (retBuilder.length() >= length) {
                break;
            }
        }
        var ret = retBuilder.toString();
        ret = ret.substring(0, length);
        return ret;
    }

    /**
     * 获取指定区间的随机数
     *
     * @param min： 大于等于 min
     * @param max： 小于 max
     * @return
     */
    public static int getRandomNumber(int min, int max) {
        var start = Math.min(min, max);
        var end = Math.max(min, max);
        var base = end - start;
        return (Math.abs(random.nextInt()) % base) + start;
    }
}
